const Sequelize = require('sequelize')
const seq = require('./utils/db_config')
const User = seq.define('user', {
  username: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "用户名"
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "密码"
  },
  icon_url: {
    type: Sequelize.STRING(10000),
    allowNull: false,
    comment: "头像地址",
    defaultValue: "/uploads/imgs/defaultIcon.png"
  },
  downloads: {
    type: Sequelize.STRING(10000),
    allowNull: false,
    comment: "下载权限",
    defaultValue: ""
  }
})
const RoleMapUser = seq.define('roleMapUser', {
  userId: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "用户Id"
  },
  roleId: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "角色Id"
  },
})
const Role = seq.define('role', {
  name: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "角色名称"
  }
})


const Article = seq.define('article', {
  title: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "标题"
  },
  content: {
    type: Sequelize.TEXT,
    allowNull: false,
    comment: "内容"
  },
  cover_url: {
    type: Sequelize.STRING(10000),//对应数据库为varchar(255)
    allowNull: true,//不能为空
    comment: "地址",
    defaultValue: '/uploads/imgs/article_background.png'
  },
  file_url: {
    type: Sequelize.STRING(10000),//对应数据库为varchar(255)
    allowNull: true,//不能为空
    comment: "文件地址",
  },
  userId: {
    type: Sequelize.INTEGER,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "关联用户---此文章是哪个用户发表的"
  }
})

const Comment = seq.define('comment', {
  title: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: true,//不能为空
    comment: "标题"
  },
  content: {
    type: Sequelize.TEXT,
    allowNull: false,
    comment: "内容"
  },
  articleId: {
    type: Sequelize.INTEGER,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "关联文章---此文章是哪个用户发表文章"
  },
  userId: {
    type: Sequelize.INTEGER,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "关联文章---此文章是哪个用户发表文章"
  },
})

const Label = seq.define('label', {
  label: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "类型"
  },
  articleId: {
    type: Sequelize.INTEGER,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "类型"
  }
})
const SystemParams = seq.define('systemParam', {
  code: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "代码",
    charset: 'utf8',
    collate: 'utf8_general_ci',
  },
  name: {
    type: Sequelize.TEXT,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "名称",
    charset: 'utf8',
    collate: 'utf8_general_ci',
  },
  value: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: false,//不能为空
    comment: "值",
    charset: 'utf8',
    collate: 'utf8_general_ci',
  },
  val1: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: true,//不能为空
    comment: "附加条件1",
    charset: 'utf8',
    collate: 'utf8_general_ci',
  },
  val2: {
    type: Sequelize.STRING,//对应数据库为varchar(255)
    allowNull: true,//不能为空
    comment: "附加条件2",
    charset: 'utf8',
    collate: 'utf8_general_ci',
  }
}, {
  charset: 'utf8',
  collate: 'utf8_general_ci',
})


Article.belongsTo(User, {//默认关联id
  foreignKey: "userId"
})
User.hasMany(Article, {
  foreignKey: "userId"
})

User.hasMany(Comment, {
  foreignKey: "userId"
})
Article.hasMany(Comment, {
  foreignKey: "articleId"
})
Comment.belongsTo(User, {//默认关联id
  foreignKey: "userId"
})
Comment.belongsTo(Article, {//默认关联id
  foreignKey: "articleId"
})
Label.belongsTo(Article, {//默认关联id
  foreignKey: "articleId"
})
Article.hasMany(Label, {
  foreignKey: "articleId"
})

User.belongsToMany(Role, {
  through: {
    model: RoleMapUser
  },
  foreignKey: 'useId'
})
Role.belongsToMany(User, {
  through: {
    model: RoleMapUser
  },
  foreignKey: 'roleId'
})


module.exports = {
  Label,
  Article,
  Comment,
  User,
  Role,
  RoleMapUser,
  SystemParams
}